#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Filename: case_list_to_xmind.py
# Author: ChenChangSong
# Date: 2025-08-24

"""
功能描述: caselist 转换为 xmind
"""
import json
import time
from function_code.xmind.generate_xmind import json_to_xmind

def ccs_case_list_to_xmind_json(title, case_list):
    # 构建根节点，使用第一个用例的需求ID和传入的标题
    root = {"title": f"需求ID：\n导出目录：{title}", "children": []}
    
    for case in case_list:
        path_str = case[1] + ' >> ' + case[2]#用例目录和名称
        # 将' >> '和'-'分割路径字符串
        parts = path_str.replace(' >> ', ',').replace('-', ',').split(',')
        current = root
        for part in parts:
            if 'children' not in current:
                current['children'] = []
                
            # 查找是否已存在该节点
            found = None
            for child in current['children']:
                if child['title'] == part:
                    found = child
                    break
                    
            if found is not None:
                current = found
            else:
                new_node = {'title': part}
                current['children'].append(new_node)
                current = new_node
                
    return root

if __name__ == '__main__':
    case_list = [
        ['1148433040001000062', '产品-Temu全托管', '外包装形状 >> 2、单-1个目录-细分主题 1', '产品 >> 1个目录', '细分主题 1'],
        ['1148433040001000062', '产品-Temu全托管', '1个目录-细分主题 2', '产品 >> 1个目录', '细分主题 2'],
        # ... 其他用例数据
    ]

    tree = ccs_case_list_to_xmind_json('【示例】日常运维', case_list)
    # print(json.dumps(tree, ensure_ascii=False, indent=4))
    current_time = time.strftime("%Y%m%d_%H%M%S", time.localtime())
    output_dir = r'function_code\xmind'
    output_path = os.path.join(output_dir, "ccstest.xmind")

    json_to_xmind(tree)  # 传递转换后的xmind_data而不是原始列表
